//
//  BorderView.m
//  CustomView
//
//  Created by hoho on 14-11-18.
//  Copyright (c) 2014年 thinkcart.net. All rights reserved.
//

#import "BorderView.h"

@implementation BorderView{
    CALayer *topBorder;
    CALayer *rightBorder;
    CALayer *bottomBorder;
    CALayer *leftBorder;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/


-(void)setBounds:(CGRect)newBounds {
    [super setBounds:newBounds];
    //比如说转为横屏，view的尺寸也改变，那么这些border应该也跟着变。
    if(topBorder){
        topBorder.frame = CGRectMake(0.0f, 0.0f, self.frame.size.width, _topWidth);
    }
    if(rightBorder){
        rightBorder.frame = CGRectMake(self.frame.size.width - _rightWidth, 0.0f, _rightWidth, self.frame.size.height);
    }
    if(bottomBorder){
        bottomBorder.frame = CGRectMake(0.0f, self.frame.size.height - _bottomWidth, self.frame.size.width, _bottomWidth);
    }
    if(leftBorder){
        leftBorder.frame = CGRectMake(0.0f, 0.0f, _leftWidth, self.frame.size.height);
    }
}

- (void)drawRect:(CGRect)rect {
    
    if(_topWidth && _topColor){
        topBorder = [CALayer layer];
        topBorder.frame = CGRectMake(0.0f, 0.0f, self.frame.size.width, _topWidth);
        topBorder.backgroundColor = _topColor.CGColor;
        [self.layer addSublayer:topBorder];
    }
    
    if(_rightWidth && _rightColor){
        rightBorder = [CALayer layer];
        rightBorder.frame = CGRectMake(self.frame.size.width - _rightWidth, 0.0f, _rightWidth, self.frame.size.height);
        rightBorder.backgroundColor = _rightColor.CGColor;
        [self.layer addSublayer:rightBorder];
    }
    
    if(_bottomWidth && _bottomColor){
        bottomBorder = [CALayer layer];
        bottomBorder.frame = CGRectMake(0.0f, self.frame.size.height - _bottomWidth, self.frame.size.width, _bottomWidth);
        bottomBorder.backgroundColor = _bottomColor.CGColor;
        [self.layer addSublayer:bottomBorder];
    }
    
    if(_leftWidth && _leftColor){
        leftBorder = [CALayer layer];
        leftBorder.frame = CGRectMake(0.0f, 0.0f, _leftWidth, self.frame.size.height);
        leftBorder.backgroundColor = _leftColor.CGColor;
        [self.layer addSublayer:leftBorder];
    }
}

@end
